Page({
  data: {
    orderType: '自提',
    currentCategory: '甜品',
    products: [
      { id: 1, name: '虎皮芋泥奶冻卷', image: '/images/product1.jpg', price: 28, category: '甜品' },
      { id: 2, name: '椰奶冻抱抱碗', image: '/images/product2.jpg', sales: 548, stock: 6, price: 18, category: '甜品' },
      { id: 3, name: '虎皮千层', image: '/images/product3.jpg', sales: 665, stock: 1, price: 20, category: '甜品' },
      { id: 4, name: '冰山熔岩巧克力', image: '/images/product4.jpg', sales: 289, stock: 15, price: 20, category: '甜品' },
      { id: 5, name: '抹茶栗子蛋糕', image: '/images/product5.jpg', sales: 38, stock: 8, price: 35, category: '甜品' },
      { id: 6, name: '单层蛋糕', image: '/images/single1.jpg', sales: 50, stock: 5, price: 50, category: '单层' },
      { id: 7, name: '双层蛋糕', image: '/images/double1.jpg', sales: 30, stock: 4, price: 80, category: '双层' },
      { id: 8, name: '多层蛋糕', image: '/images/multi1.jpg', sales: 20, stock: 2, price: 120, category: '多层' }
    ],
    filteredProducts: [],
    showProductDetail: false,
    selectedProduct: null,
    showCartPopup: false
  },
  onLoad() {
    this.filterCategory({ currentTarget: { dataset: { category: '甜品' } } });
  },
  changeOrderType(e) {
    this.setData({
      orderType: e.detail.value
    });
  },
  filterCategory(e) {
    const category = e.currentTarget.dataset.category;
    const filteredProducts = this.data.products.filter(product => product.category === category || (category === '蛋糕' && ['单层', '双层', '多层'].includes(product.category)));
    this.setData({ currentCategory: category, filteredProducts });
  },
  showProductDetail(e) {
    const id = e.currentTarget.dataset.id;
    const product = this.data.products.find(p => p.id === id);
    this.setData({
      showProductDetail: true,
      selectedProduct: product
    });
  },
  closeProductDetail() {
    this.setData({
      showProductDetail: false,
      selectedProduct: null
    });
  },
  showCartPopup(e) {
    const id = e.currentTarget.dataset.id;
    const product = this.data.products.find(p => p.id === id);
    this.setData({
      showCartPopup: true,
      selectedProduct: product
    });
  },
  closeCartPopup() {
    this.setData({
      showCartPopup: false,
      selectedProduct: null
    });
  },
  addToCart(e) {
    // 实现加入购物车的逻辑
  },
  goToCart() {
    wx.switchTab({
      url: '/pages/orders/orders'
    });
  },
  goToOrderCenter() {
    wx.navigateTo({
      url: '/pages/orderCenter/orderCenter'
    });
  }
})
